package javase.javase02.thread;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author: Gan
 * @date 2021/4/22 0022 17:07
 * @description: 3.创建一个任务，它将睡眠随机1-10s， 然后显示睡眠时间并退出。创建并运行一定数量的这个任务
 * ps:利用线程池
 **/
public class Day21Work03 {
    public static void main(String[] args) {
        //
        ExecutorService pool = Executors.newFixedThreadPool(3);
        //随机睡眠的时间
        pool.submit(new Mr(new Random().nextInt(11)));
        pool.submit(new Mr(new Random().nextInt(11)));
        pool.submit(new Mr(new Random().nextInt(11)));
        pool.shutdown();
    }
}

class Mr implements Runnable {
    private int sleepTime;

    public Mr(int sleepTime) {
        this.sleepTime = sleepTime;
    }

    @Override
    public void run() {
        try {
            TimeUnit.SECONDS.sleep(sleepTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("休眠时间长度是："+sleepTime);
    }
}